<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" inline v-show="showSearch">
      <el-form-item label="客户名称" prop="like.customer_name" class="w-200px">
        <el-input v-model="searchForm.like.customer_name" placeholder="请输入客户名称" clearable @keyup.enter="search" />
      </el-form-item>
      <el-form-item label="联系人" prop="like.contact_person" class="w-180px">
        <el-input v-model="searchForm.like.contact_person" placeholder="请输入联系人" clearable @keyup.enter="search" />
      </el-form-item>
      <el-form-item label="状态" prop="where.status" class="w-180px">
        <el-select v-model="searchForm.where.status" placeholder="请选择状态" clearable>
          <el-option label="正常" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="search">搜索</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px w-full">
      <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      <el-button type="success" plain icon="Download" @click="handleExport">导出</el-button>
      <right-toolbar v-model:showSearch="showSearch" @refresh="getList" v-model:columns="columns" />
    </div>

    <el-table v-loading="loading" :data="listData">
      <template v-for="item in columns">
        <el-table-column
          align="center"
          :key="item.key"
          :prop="item.key"
          :label="item.label"
          :show-overflow-tooltip="!!item.overflow"
          :width="item.width"
          v-if="item.show">
          <template v-slot="{ row }">
            <span v-if="item.key === 'status'">
              <el-tag v-if="row.status === 1" type="success">正常</el-tag>
              <el-tag v-else type="info">禁用</el-tag>
            </span>
            <span v-else-if="item.key === 'customer_level'">
              <el-rate v-model="row.customer_level" disabled text-color="#ff9900" />
            </span>
            <span v-else>{{ row[item.key] }}</span>
          </template>
        </el-table-column>
      </template>

      <el-table-column label="操作" align="center" width="240" fixed="right">
        <template v-slot="{ row }">
          <el-button link type="primary" icon="View" @click="handleView(row)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleEdit(row)">编辑</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="page" v-model:limit="limit" @pagination="getList" />

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customer_name">
              <el-input v-model="form.customer_name" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="customer_no">
              <el-input v-model="form.customer_no" placeholder="自动生成" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact_person">
              <el-input v-model="form.contact_person" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contact_phone">
              <el-input v-model="form.contact_phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系邮箱" prop="contact_email">
              <el-input v-model="form.contact_email" placeholder="请输入联系邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户等级" prop="customer_level">
              <el-rate v-model="form.customer_level" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="公司名称" prop="company_name">
          <el-input v-model="form.company_name" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司地址" prop="company_address">
          <el-input v-model="form.company_address" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="客户详情" v-model="detailVisible" width="700px" append-to-body>
      <el-descriptions :column="2" border v-if="detailData">
        <el-descriptions-item label="客户名称">{{ detailData.customer_name }}</el-descriptions-item>
        <el-descriptions-item label="客户编号">{{ detailData.customer_no }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ detailData.contact_person }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ detailData.contact_phone }}</el-descriptions-item>
        <el-descriptions-item label="联系邮箱" :span="2">{{ detailData.contact_email }}</el-descriptions-item>
        <el-descriptions-item label="客户等级">
          <el-rate v-model="detailData.customer_level" disabled text-color="#ff9900" />
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="detailData.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="info">禁用</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="公司名称" :span="2">{{ detailData.company_name || '无' }}</el-descriptions-item>
        <el-descriptions-item label="公司地址" :span="2">{{ detailData.company_address || '无' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailData.remark || '无' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailData.created_at }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ detailData.updated_at }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailVisible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CustomerList">
import { listCustomer, getCustomer, addCustomer, updateCustomer, delCustomer } from '@/api/license/customer'

const { proxy } = getCurrentInstance()

const listData = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const page = ref(1)
const limit = ref(10)

const searchForm = ref({
  like: {
    customer_name: '',
    contact_person: ''
  },
  where: {
    status: ''
  }
})

const columns = ref([
  { key: 'id', label: 'ID', width: 80, show: true },
  { key: 'customer_no', label: '客户编号',  width: 200, show: true },
  { key: 'customer_name', label: '客户名称',show: true, overflow: true },
  { key: 'contact_person', label: '联系人', width: 120, show: true },
  { key: 'contact_phone', label: '联系电话', width: 130, show: true },
  { key: 'contact_email', label: '联系邮箱', width: 180, show: true, overflow: true },
  { key: 'customer_level', label: '客户等级', width: 200, show: true },
  { key: 'status', label: '状态', width: 100, show: true }
])

const searchFormRef = ref()
const formRef = ref()
const dialogVisible = ref(false)
const dialogTitle = ref('')
const submitLoading = ref(false)
const detailVisible = ref(false)
const detailData = ref(null)

const form = ref({
  id: null,
  customer_name: '',
  customer_no: '',
  contact_person: '',
  contact_phone: '',
  contact_email: '',
  customer_level: 3,
  company_name: '',
  company_address: '',
  status: 1,
  remark: ''
})

const rules = {
  customer_name: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
  contact_person: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  contact_phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  contact_email: [
    { required: true, message: '请输入联系邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

/** 查询列表 */
function getList() {
  loading.value = true
  const params = {
    page: page.value,
    limit: limit.value,
    like: searchForm.value.like,
    where: searchForm.value.where
  }
  listCustomer(params)
    .then(response => {
      listData.value = response.list || []
      total.value = response.page?.total || 0
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

/** 搜索按钮操作 */
function search() {
  page.value = 1
  getList()
}

/** 重置按钮操作 */
function reset() {
  searchFormRef.value?.resetFields()
  search()
}

/** 重置表单 */
function resetForm() {
  form.value = {
    id: null,
    customer_name: '',
    customer_no: '',
    contact_person: '',
    contact_phone: '',
    contact_email: '',
    customer_level: 3,
    company_name: '',
    company_address: '',
    status: 1,
    remark: ''
  }
  formRef.value?.resetFields()
}

/** 新增按钮操作 */
function handleAdd() {
  resetForm()
  dialogTitle.value = '新增客户'
  dialogVisible.value = true
}

/** 查看详情 */
function handleView(row) {
  getCustomer({ id: row.id }).then(res => {
    detailData.value = res
    detailVisible.value = true
  })
}

/** 修改按钮操作 */
function handleEdit(row) {
  resetForm()
  getCustomer({ id: row.id }).then(res => {
    form.value = { ...res }
    dialogTitle.value = '编辑客户'
    dialogVisible.value = true
  })
}

/** 提交表单 */
function submitForm() {
  formRef.value.validate(valid => {
    if (!valid) return

    submitLoading.value = true
    const submitFn = form.value.id ? updateCustomer : addCustomer
    submitFn(form.value)
      .then(() => {
        proxy.$modal.msgSuccess(form.value.id ? '修改成功' : '新增成功')
        dialogVisible.value = false
        getList()
      })
      .finally(() => {
        submitLoading.value = false
      })
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除该客户？')
    .then(() => {
      return delCustomer({ id: row.id })
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.$modal.msgWarning('导出功能开发中...')
}

onMounted(() => {
  getList()
})
</script>
